package org.example.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class TreeMethod {
    /**
     * 层序遍历
     * @param root
     * @return
     */
    public static List<Integer> levelOrder(TreeNode root) {
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        // 初始化列表，保存遍历数据
        ArrayList<Integer> list = new ArrayList<>();
        while (!queue.isEmpty()) {
            TreeNode pollNode = queue.poll();
            list.add(pollNode.val);
            if (null != pollNode.left) {
                queue.offer(pollNode.left);
            }
            if (null != pollNode.right) {
                queue.offer(pollNode.right);
            }
        }
        return list;
    }




    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);

        node1.left = node2;
        node1.right = node3;

        node2.left = node4;
        node2.right = node5;

        node3.left = node6;
        node3.right = node7;
        /**
         *              1
         *         2           3
         *      4     5    6        7
         */


        List<Integer> integers = levelOrder(node1);
        System.out.println(integers);
    }
}
